<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8" />
        <title>PythonTutorials - Maxiaoan</title>
        <link rel="stylesheet" href="/theme/css/main.css" />

        <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
</head>

<body id="index" class="home">
        <header id="banner" class="body">
                <h1><a href="/">PythonTutorials </a></h1>
                <nav><ul>
                    <li><a href="/category/cpp.html">cpp</a></li>
                    <li><a href="/category/python.html">python</a></li>
                </ul>
                </nav>
        </header><!-- /#banner -->

                <section id="content" class="body">
                    <ol id="posts-list" class="hfeed" start="2">
            <li><article class="hentry">
                <header>
                    <h1><a href="/Part.1.E.1.entrance.html" rel="bookmark"
                           title="Permalink to Part.1.E.1.entrance">Part.1.E.1.entrance</a></h1>
                </header>

                <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/python.html">python</a><a href="/tag/progarmming.html">progarmming</a><a href="/tag/excel.html">Excel</a></span>
</footer><!-- /.post-info -->                <h1>入口</h1>
<p>“速成”，对绝大多数人<a href='#fn1' name='fn1b'><sup>[1]</sup></a>来说，在绝大多数情况下，是不大可能的。</p>
<p>编程如此，自学编程更是如此。有时，遇到复杂度高一点的知识，连快速入门都不一定是很容易的事情。</p>
<p>所以，这一章的名称，特意从 “<em>入门</em>” 改成了 “<strong>入口</strong>” —— 它的作用是给你 “指一个入口”，至于你能否从那个入口进去，是你自己的事了……</p>
<p>不过，有一点不一样的地方，我给你指出的入口，跟别的编程入门书籍不一样 —— 它们几乎无一例外都是从一个 “Hello World!” 程序开始的…… 而我们呢？</p>
<p>让我们从认识一个人开始罢……</p>
<h2>乔治・布尔</h2>
<p>1833 年，一个 18 岁的英国小伙脑子里闪过一个念头：</p>
<blockquote>
<p><strong>逻辑关系</strong>应该能用<strong>符号</strong>表示。</p>
</blockquote>
<p>这个小伙子叫乔治・布尔（<a href="https://en.wikipedia.org/wiki/George_Boole">George Boole</a>，其实之前就提到过我的这位偶像），于 1815 …</p>
                <a class="readmore" href="/Part.1.E.1.entrance.html">read more</a>
                </div><!-- /.entry-content -->
            </article></li>

            <li><article class="hentry">
                <header>
                    <h1><a href="/Part.1.E.2.values-and-their-operators.html" rel="bookmark"
                           title="Permalink to Part.1.E.2.values-and-their-operators">Part.1.E.2.values-and-their-operators</a></h1>
                </header>

                <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/python.html">python</a><a href="/tag/progarmming.html">progarmming</a><a href="/tag/excel.html">Excel</a></span>
</footer><!-- /.post-info -->                <h1>值及其相应的运算</h1>
<p>从结构上来看，一切的计算机程序，都由且只由两个最基本的成分构成：</p>
<blockquote>
<ul>
<li><strong>运算</strong>（Evaluation）</li>
<li><strong>流程控制</strong>（Control Flow）</li>
</ul>
</blockquote>
<p>没有流程控制的是计算器而已；有流程控制的才是可编程设备。</p>
<p>看看之前我们见过的计算质数的程序：（按一下 <code>⎋</code>，即 <code>ESC</code>，确保已经进入命令模式，<code>⇧ L</code> 可以切换是否显示代码行号）</p>
<div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">is_prime</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>            <span class="c1"># 定义 is_prime()，接收一个参数</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="p">:</span>              <span class="c1"># 开始使用接收到的那个参数（值）开始计算……</span>
        <span class="k">return</span> <span class="bp">False</span>       <span class="c1"># 不再是返回给人，而是返回给调用它的代码……</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">return</span> <span class="bp">True</span>
    <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">n</span><span class="o">**</span><span class="mf">0.5</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
        <span class="k">if …</span></pre></div>
                <a class="readmore" href="/Part.1.E.2.values-and-their-operators.html">read more</a>
                </div><!-- /.entry-content -->
            </article></li>

            <li><article class="hentry">
                <header>
                    <h1><a href="/Part.1.E.3.controlflow.html" rel="bookmark"
                           title="Permalink to Part.1.E.3.controlflow">Part.1.E.3.controlflow</a></h1>
                </header>

                <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/python.html">python</a><a href="/tag/progarmming.html">progarmming</a><a href="/tag/excel.html">Excel</a></span>
</footer><!-- /.post-info -->                <h1>流程控制</h1>
<p>在相对深入了解了值的基本操作之后，我们需要再返回来对流程控制做更深入的了解。</p>
<p>之前我们看过这个寻找质数的程序：</p>
<div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">100</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
        <span class="k">continue</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="n">n</span> <span class="o">%</span> <span class="n">i</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">break</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">print</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
</pre></div>


<p>这其中，包含了<em>分支</em>与<em>循环</em> —— 无论多复杂的流程控制用这两个东西就够了，就好像无论多复杂的电路最终都是由通路和开路仅仅两个状态构成的一样。</p>
<blockquote>
<p>今天的人们觉得这是 “天经地义” 的事情，可实际上并非如此。这是 1966 年的一篇论文所带来的巨大改变 —— <em>Flow diagrams, turing machines and languages …</em></p></blockquote>
                <a class="readmore" href="/Part.1.E.3.controlflow.html">read more</a>
                </div><!-- /.entry-content -->
            </article></li>
            </ol><!-- /#posts-list -->
<p class="paginator">
        <a href="/author/maxiaoan2.html">&laquo;</a>
    Page 3 / 22
        <a href="/author/maxiaoan4.html">&raquo;</a>
</p>
            </section><!-- /#content -->
        <section id="extras" class="body">
                <div class="blogroll">
                        <h2>blogroll</h2>
                        <ul>
                            <li><a href="http://getpelican.com/">Pelican</a></li>
                            <li><a href="http://python.org/">Python.org</a></li>
                            <li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
                            <li><a href="#">You can modify those links in your config file</a></li>
                        </ul>
                </div><!-- /.blogroll -->
                <div class="social">
                        <h2>social</h2>
                        <ul>

                            <li><a href="#">You can add links in your config file</a></li>
                            <li><a href="#">Another social link</a></li>
                        </ul>
                </div><!-- /.social -->
        </section><!-- /#extras -->

        <footer id="contentinfo" class="body">
                <p>Powered by <a href="http://www.maxiaoan.com/">Maxiaoan</a></p>
        </footer><!-- /#contentinfo -->

</body>
</html>